//
//  NewMomentView.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

struct NewMomentView: View {
    @EnvironmentObject var discoverViewModel: DiscoverViewModel
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                // 文本输入区域
                TextEditor(text: $discoverViewModel.newMomentText)
                    .font(.system(size: 16))
                    .padding()
                    .background(Color(UIColor.systemBackground))
                
                Divider()
                
                // 图片选择区域
                ScrollView {
                    LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 8), count: 3), spacing: 8) {
                        ForEach(Array(discoverViewModel.selectedImages.enumerated()), id: \.offset) { index, imageName in
                            ZStack(alignment: .topTrailing) {
                                Image(systemName: imageName)
                                    .font(.system(size: 30))
                                    .foregroundColor(.blue)
                                    .frame(width: 80, height: 80)
                                    .background(Color.gray.opacity(0.2))
                                    .clipShape(RoundedRectangle(cornerRadius: 8))
                                
                                Button(action: {
                                    discoverViewModel.removeImage(at: index)
                                }) {
                                    Image(systemName: "xmark.circle.fill")
                                        .font(.system(size: 16))
                                        .foregroundColor(.red)
                                        .background(Color.white)
                                        .clipShape(Circle())
                                }
                                .offset(x: 8, y: -8)
                            }
                        }
                        
                        // 添加图片按钮
                        if discoverViewModel.selectedImages.count < 9 {
                            Button(action: {
                                addSampleImage()
                            }) {
                                Image(systemName: "plus")
                                    .font(.system(size: 30))
                                    .foregroundColor(.gray)
                                    .frame(width: 80, height: 80)
                                    .background(Color.gray.opacity(0.1))
                                    .clipShape(RoundedRectangle(cornerRadius: 8))
                            }
                        }
                    }
                    .padding()
                }
                
                Spacer()
            }
            .navigationTitle("发表朋友圈")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("取消") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("发表") {
                        discoverViewModel.publishMoment()
                        presentationMode.wrappedValue.dismiss()
                    }
                    .disabled(discoverViewModel.newMomentText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
                }
            }
        }
    }
    
    private func addSampleImage() {
        let sampleImages = ["photo", "photo.fill", "camera", "camera.fill", "image", "doc.richtext", "video", "music.note", "app.gift"]
        if let randomImage = sampleImages.randomElement() {
            discoverViewModel.addImage(randomImage)
        }
    }
}
